// pages/wuliurongqiguanli/nav_check_gzcbl/check_gzcbl.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {

    baseUrl: "https://logisticsengineering.gree.com/map_api",
    list: [
      {
        name: "珠海凌达",
        code: "600",
      },
      {
        name: "郑州凌达",
        code: "330",
      },
      {
        name: "合肥凌达",
        code: "306",
      },
      {
        name: "武汉凌达",
        code: "566",
      },
      {
        name: "重庆凌达",
        code: "506",
      },

    ],
    GreeCode: "",
    batch_num: "",
    material_code_card: "",
    Company_str: "",
    search_disabled: true,
  },
  onInput(e) {
    if (e.detail == "") {
      this.setData({
        GreeCode: ""
      })
    }
    this.checkNullValue()
  },
  // 扫描
  scan1(e) {
    this.setData({
      GreeCode: "",
      batch_num: "",
      material_code_card: "",
    })
    let key = e.currentTarget.dataset.key
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code,
        })
        if (key == 'material_code_card') {
          self.getStandardDatas(label_code)
        }
        if (key == 'batch_num') {
          self.getDDDatas(label_code)
        }
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },
  // 根据物料配送看板--物料编码模板--数据
  getStandardDatas() {
    wx.showLoading({
      title: '处理中...',
    })
    let url = "https://logisticsengineering.gree.com/pfep2_api/standard_doc_store_manage/transfer_batch2material_num2"
    let data = {
      batch_num: this.data.material_code_card,
      use_in: "",
    }
    console.log("传过去的数据", data);
    this.post(url, data).then(res => {

      if (res.code == 20000) {
        wx.hideLoading()
       if (res.data.material_num !="") {
        this.setData({
          GreeCode: res.data.material_num
        })
       }else{
        wx.showToast({
          title: "查无数据",
          icon: 'error'
        })
       }
        this.checkNullValue()

      } else {
        this.setData({
          GreeCode: ""
        })
        wx.hideLoading()
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        GreeCode: ""
      })
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 根据订单号--物料编码模板--数据
  getDDDatas() {
    wx.showLoading({
      title: '处理中...',
    })
    let url = this.data.baseUrl + "/mlbm_datas";
    let data = {
      ddho: this.data.batch_num,
      wlzu: "550",
    }
    this.post(url, data).then(res => {
      console.log(res);
      if (res.code == 20000) {
        wx.hideLoading()
       if (res.data.table_data.length !=0) {
        this.setData({
          GreeCode: res.data.table_data[0].material_code
        })
       }else{
        wx.showToast({
          title: '查无数据',
          icon: 'error'
        })
       }
        this.checkNullValue()

      } else {
        this.setData({
          GreeCode: ""
        })
        wx.hideLoading()
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }

    }).catch(err => {
      this.setData({
        GreeCode: ""
      })
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 查询-
  getData() {
    wx.showLoading({
      title: '获取中...',
    })
    let url = this.data.baseUrl + "/ysj_datas";
    let data = {
      GreeCode: this.data.GreeCode,
      Company: this.data.Company,
    }

    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {
        let arr  = res.data.table_data.fuJian
        const uniqueList = Array.from(
          new Map(arr.map(item => [item.partCode, item])).values()
        );
        for (let i = 0; i < uniqueList.length; i++) {
          if (uniqueList[i].proportion < 1) {
            uniqueList.splice(i,1)
          }
          
        }
        this.setData({
          data_list: uniqueList
        })

        if (res.data.table_data.fuJian.length == 0) {
          wx.showToast({
            title: '无数据',
            icon: "error"
          })
        }

        wx.hideLoading()
      } else {
        wx.hideLoading()
        this.setData({
          data_list: []
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.hideLoading()
      this.setData({
        data_list: []
      })
      wx.showToast({
        title: "查无信息",
        icon: 'none'
      })
    })
  },
  getPickerVlaue(e) {
    console.log(e);
    this.setData({
      Company: e.detail.code,
      Company_str: e.detail.name,
    })
    this.checkNullValue()
  },
  checkNullValue() {
    if (this.data.GreeCode == "") {
      this.setData({
        search_disabled: true
      })
    } else {
      this.setData({
        search_disabled: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})